package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PUserLevelBo;
import com.ruoyi.system.domain.vo.PUserLevelVo;
import com.ruoyi.system.domain.PUserLevel;
import com.ruoyi.system.mapper.PUserLevelMapper;
import com.ruoyi.system.service.IPUserLevelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 普通会员等级Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@RequiredArgsConstructor
@Service
public class PUserLevelServiceImpl implements IPUserLevelService {

    private final PUserLevelMapper baseMapper;

    /**
     * 查询普通会员等级
     */
    @Override
    public PUserLevelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询普通会员等级列表
     */
    @Override
    public TableDataInfo<PUserLevelVo> queryPageList(PUserLevelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PUserLevel> lqw = buildQueryWrapper(bo);
        Page<PUserLevelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询普通会员等级列表
     */
    @Override
    public List<PUserLevelVo> queryList(PUserLevelBo bo) {
        LambdaQueryWrapper<PUserLevel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PUserLevel> buildQueryWrapper(PUserLevelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PUserLevel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PUserLevel::getName, bo.getName());
        lqw.eq(bo.getExperience() != null, PUserLevel::getExperience, bo.getExperience());
        lqw.eq(bo.getIsShow() != null, PUserLevel::getIsShow, bo.getIsShow());
        lqw.eq(bo.getGrade() != null, PUserLevel::getGrade, bo.getGrade());
        lqw.eq(bo.getDiscount() != null, PUserLevel::getDiscount, bo.getDiscount());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), PUserLevel::getIcon, bo.getIcon());
        return lqw;
    }

    /**
     * 新增普通会员等级
     */
    @Override
    public Boolean insertByBo(PUserLevelBo bo) {
        PUserLevel add = BeanUtil.toBean(bo, PUserLevel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改普通会员等级
     */
    @Override
    public Boolean updateByBo(PUserLevelBo bo) {
        PUserLevel update = BeanUtil.toBean(bo, PUserLevel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PUserLevel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除普通会员等级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
